<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.name" placeholder="班级名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button @click="selectVisible = true">高级查询</el-button>
        <el-button v-if="isAuth(':edustudent:save')" type="primary" @click="dataReset()">重置</el-button>
        <el-button v-if="isAuth(':educlass:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth(':educlass:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        type="index"
        header-align="center"
        align="center"
        label="序号"
        width="50">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="班级名称"
        width="280"
        show-overflow-tooltip>
        <template slot-scope="scope">
          <div style="color:#17B3A3;cursor: pointer;" @click="goDetail(scope.row.id)" >{{scope.row.name}}</div>
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态">
        <template slot-scope="scope">
          <!-- <div v-if="scope.row.status == 0">草稿</div> -->
          <div v-if="scope.row.status == 1">发布</div>
          <div v-if="scope.row.status == 2">作废</div>
          <div v-if="scope.row.status == 5">其他</div>
        </template>
      </el-table-column>
      <el-table-column
        prop="price"
        header-align="center"
        align="center"
        label="销售价格(元)"
        width="150">
      </el-table-column>
      <el-table-column
        prop="startTime"
        header-align="center"
        align="center"
        label="开始时间"
        width="160">
      </el-table-column>
      <el-table-column
        prop="endTime"
        header-align="center"
        align="center"
        label="结束时间"
        width="160">
      </el-table-column>
      <el-table-column
        prop="year"
        header-align="center"
        align="center"
        label="年度"
        width="120">
      </el-table-column>
      <el-table-column
        prop="schoolName"
        header-align="center"
        align="center"
        label="校区"
        width="120">
      </el-table-column>
      <el-table-column
        prop="teacherName"
        header-align="center"
        align="center"
        label="教师">
      </el-table-column>
      <el-table-column
        prop="subjectName"
        header-align="center"
        align="center"
        label="课程科目">
      </el-table-column>
      <el-table-column
        prop="classTypeName"
        header-align="center"
        align="center"
        label="班级类型">
      </el-table-column>
      <el-table-column
        prop="categoryName"
        header-align="center"
        align="center"
        label="课程分类">
      </el-table-column>
      <el-table-column
        prop="gradeName"
        header-align="center"
        align="center"
        label="年级"
        width="120">
      </el-table-column>
      <el-table-column
        prop="lessonNum"
        header-align="center"
        align="center"
        label="总课时">
      </el-table-column>
      <el-table-column
        prop="restCount"
        header-align="center"
        align="center"
        label="剩余名额">
      </el-table-column>
      <el-table-column
        prop="totalNum"
        header-align="center"
        align="center"
        label="总人数">
      </el-table-column>
      <el-table-column
        prop="score"
        header-align="center"
        align="center"
        label="要求分数">
      </el-table-column>
      <el-table-column
        prop="isOver"
        header-align="center"
        align="center"
        label="是否结束">
        <template slot-scope="scope">
          <div v-if="scope.row.isOver == 0">否</div>
          <div v-if="scope.row.isOver == 1">是</div>
        </template>
      </el-table-column>
      <el-table-column
        prop="createUserName"
        header-align="center"
        align="center"
        label="创建人">
      </el-table-column>
       <el-table-column
        prop="gmtCreate"
        header-align="center"
        align="center"
        width="160"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="modifiedUserName"
        header-align="center"
        align="center"
        
        label="修改人">
      </el-table-column>
      <el-table-column
        prop="gmtModified"
        header-align="center"
        align="center"
        width="160"
        label="修改时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="newAddHandle(scope.row.id)">新增</el-button>
          <el-button type="text" size="small" @click="exportStudent(scope.row.id)">导出</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 筛选弹窗 -->
    <el-dialog
      title="筛选"
      :close-on-click-modal="false"
      :visible.sync="selectVisible">
      <el-form :model="dataForm"  ref="dataSelectForm" label-width="80px">
        <el-form-item label="分数" prop="score">
          <el-input v-model="dataForm.score" placeholder="学员分数"></el-input>
        </el-form-item>
        <el-form-item label="年级" prop="gradeId">
          <el-select v-model="dataForm.gradeId"  placeholder="请选择">
              <el-option
              v-for="item in gradeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
              </el-option>
          </el-select>
        </el-form-item>
         <el-form-item label="科目" prop="subjectId">
          <el-select v-model="dataForm.subjectId"  placeholder="请选择">
              <el-option
              v-for="item in subjectOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
              </el-option>
          </el-select>
        </el-form-item>
         <el-form-item label="校区" prop="schoolId">
          <el-select v-model="dataForm.schoolId"  placeholder="请选择">
              <el-option
              v-for="item in schoolOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
              </el-option>
          </el-select>
        </el-form-item>
         <el-form-item label="分类" prop="categoryId">
          <el-select v-model="dataForm.categoryId"  placeholder="请选择">
              <el-option
              v-for="item in categoryOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
              </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班型" prop="classTypeId">
          <el-select v-model="dataForm.classTypeId"  placeholder="请选择">
              <el-option
              v-for="item in classTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
              </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelForm()">取消</el-button>
        <el-button type="primary" @click="getDataList()">确定</el-button>
      </span>
    </el-dialog>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './educlass_add_or_update'
  export default {
    data () {
      return {
        dataForm: {
          name: '',
          score: '',
          gradeId: '',
          subjectId: '',
          schoolId: '',
          categoryId: '',
          classTypeId: '',
        },
        schoolOptions: [],
        subjectOptions: [],
        classTypeOptions: [],
        categoryOptions: [],
        gradeOptions: [],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        selectVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList();
      this.getCommonList();
    },
    methods: {
       //获取公共列表
      getCommonList() {
          this.$http({
            url: this.$http.adornUrl('/sys/common/list'),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if(data && data.code === 0) {
                let schoolOptions = data.school || [];
                let subjectOptions = data.subject || [];
                let classTypeOptions = data.classType || [];
                let categoryOptions = data.category || [];
                let gradeOptions = data.grade || [];
                // let teacherOptions = data.teacher || [];
                schoolOptions.forEach((item) => {
                    item.value = item.id;
                    item.label = item.name;
                })
                subjectOptions.forEach((item) => {
                    item.value = item.id;
                    item.label = item.name;
                })
                classTypeOptions.forEach((item) => {
                    item.value = item.id;
                    item.label = item.name;
                })
                categoryOptions.forEach((item) => {
                    item.value = item.id;
                    item.label = item.name;
                })
                gradeOptions.forEach((item) => {
                    item.value = item.id;
                    item.label = item.name;
                })
                // teacherOptions.forEach((item) => {
                //     item.value = item.id;
                //     item.label = item.name;
                // })

                this.schoolOptions = schoolOptions;
                this.subjectOptions = subjectOptions;
                this.classTypeOptions = classTypeOptions;
                this.categoryOptions = categoryOptions;
                this.gradeOptions = gradeOptions;
                // this.teacherOptions = teacherOptions;

            }
          })
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sys/educlass/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'name': this.dataForm.name,
            'score': this.dataForm.score != '' ? Number(this.dataForm.score) : '',
            'gradeId': this.dataForm.gradeId != '' ? Number(this.dataForm.gradeId) : '',
            'subjectId': this.dataForm.subjectId != '' ? Number(this.dataForm.subjectId) : '',
            'schoolId': this.dataForm.schoolId != '' ?Number(this.dataForm.schoolId) : '',
            'categoryId': this.dataForm.categoryId != '' ? Number(this.dataForm.categoryId) : '',
            'classTypeId': this.dataForm.classTypeId != '' ? Number(this.dataForm.classTypeId) : '',
            'isDeleted':0
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
          this.selectVisible = false
        })
      },
      dataReset() {
        this.dataForm = {
         name: '',
          score: '',
          gradeId: '',
          subjectId: '',
          schoolId: '',
          categoryId: '',
          classTypeId: '',
        }
        this.getDataList();
      },
       cancelForm() {
        // this.loading = false;
        this.selectVisible = false;
        this.dataForm = {
         name: '',
          score: '',
          gradeId: '',
          subjectId: '',
          schoolId: '',
          categoryId: '',
          classTypeId: '',
        }
      },
      goDetail(classId) {
        this.$router.push({path:'/classDetail', query: {classId}})
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 修改
      addOrUpdateHandle (id) {
        this.$router.push({path:'/addEduclass', query: {id,type: 'edite'}})
      
      },
      //新增复制
      newAddHandle(id) {
        this.$router.push({path: '/addEduclass', query: {id,type: 'add'}})
      },
      //导出
      exportStudent(id) {
        this.$confirm(`是否要导出该班级的学生信息?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(()=>{
          this.$http({
            url: this.$http.adornUrl(`/sys/edustudent/download/${id}/student`),
            method: 'get',
            data: this.$http.adornParams({"classId":id}),
            responseType: "blob"
          }).then(res => {
            const link = document.createElement("a");
            let blob = new Blob([res.data], { type: "multipary/form-data" });
            link.style.display = "none";
            link.href = URL.createObjectURL(blob);
            link.setAttribute("download", decodeURIComponent(res.headers.filename));
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
            console.log(res);
          })
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定进行${id ? '删除' : '批量删除'}操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/sys/educlass/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
